package com.boot.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boot.entity.Children;
import com.boot.entity.Employ;
import com.boot.entity.Result;
import com.boot.entity.Student;
import com.boot.entity.TreeNode;
import com.boot.mapper.ActivitiMapper;

@Controller
@RequestMapping("hello")
public class BootController {
	 @Value("${com.boot.username}")
	 String username;
	 @Autowired
	 ActivitiMapper mapper;
	 @RequestMapping("insertStudent")
	 public @ResponseBody String insertStudent() {
		 Student stu = new Student();
		 stu.setUsername("hello7");
		 stu.setPassword("123");
		 mapper.insertUser(stu);
		 return "ojbk"+username;
	 }
	
	 //获取所有员工信息
	 @RequestMapping("getAllEmploy")
	 public @ResponseBody List<Employ> getAllEmploy(@RequestParam(value="searchString",required=false)String query,@RequestParam(name="searchField",required=false)String searchField){
		List<Employ> list = null;
		if(searchField==null) {
			System.out.println("传参为空");
			list = mapper.selectAllEmploy();
		} 
		else {
			if(searchField.equals("employName")) {
				list =  mapper.selectEmployByName(query);
			}
			if(searchField.equals("tax")) {
				list =  mapper.selectEmployById(Integer.parseInt(query));
			}
			
		}
		
		return list;
	 }
	//变更员工信息
	 @RequestMapping("deleteEmploy")
	 public @ResponseBody Result deleteEmploy(String oper,HttpServletRequest request,@RequestParam(name="adress",required=false)String address) {
		 System.out.println(oper);
		 Result result = new Result();
		 if(oper.equals("del")) {
			int id = Integer.parseInt(request.getParameter("id"));
			System.out.println("删除的工号"+id);
			mapper.deleteEmployById(id);
		 }
		 if(oper.equals("add")) {
			 String employName = request.getParameter("employName");
			 String adress = request.getParameter("adress");
			 String age = request.getParameter("age");
			 String phone = request.getParameter("phone");
			 String captor = request.getParameter("captor");
			 Random num = new Random();
			 int emId = num.nextInt(6000);
			 Employ employ = new Employ();
			 employ.setAdress(adress);
			 employ.setAge(age);
			 employ.setCaptor(captor);
			 employ.setEmployName(employName);
			 employ.setPhone(phone);
			 employ.setEmployId(emId);
			 mapper.insertEmploy(employ);
		 }
		 if(oper.equals("edit")) {
			 int id = Integer.parseInt(request.getParameter("id"));
			 System.out.println(id);
			 String employName = request.getParameter("employName");
			 String adress = request.getParameter("adress");
			 String age = request.getParameter("age");
			 System.out.println(age);
			 String phone = request.getParameter("phone");
			 String captor = request.getParameter("captor");
			 Employ employ = new Employ();
			 employ.setAdress(adress);
			 employ.setAge(age);
			 employ.setCaptor(captor);
			 employ.setEmployName(employName);
			 employ.setPhone(phone);
			 employ.setEmployId(id);
			 mapper.updateEmploy(employ);
		 }
		 result.setResult("操作成功");
		 return result;
	 }
	 
	 @RequestMapping("getNum")
	    public @ResponseBody List<TreeNode> getNum() {
	    	List<TreeNode> node = new ArrayList<TreeNode>();
	    	List<Children> lit = new ArrayList<Children>();
	    	for(int i=0;i<4;i++) {
	    		Children chi = new Children();
	    		chi.setName("子节点"+i);
	    		lit.add(chi);
	    	}
	    	for(int i=0;i<4;i++) {
	    		TreeNode tr = new TreeNode();
	    		tr.setChildren(lit);
	    		tr.setName("父节点"+i);
	    		node.add(tr);
	    	}
	    	
	    	
	    	return node;
	    }
	 
	 @RequestMapping("hello1")
     public String hello() {
		 return "async";
	 }
	 
	 @Resource(name="bean1")
	 Employ employ;
	 
	 @RequestMapping("beanTest")
	 public @ResponseBody Employ beanTest(){
		 return employ;
	 }
	 
	 
}
